/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 数字或金额输入控件
 * 使用说明：
 * jsp中使用范例：最常用模式：<htdz-tag:InputTag name="mymemo"/>
 * 
 * 参数说明：
 * 1. name为必填项
 * 2. id可以不写，缺省值与name值相同
 * 3. type可以不写，缺省值为num，可选择的值有：
 *        num：纯数字
 *        currency:货币（数字和小数点）
 *        string:数字字符串(允许首位为0)
 * 4. onchange可以不写
 * 5. size可以不写，缺省值为8
 * 6. maxlength可以不写，缺省值为7
 *    如果size或maxlength只有一个值，则size=maxlength
 * 7. disabled可以不写，缺省值为false
 * 8. onkeyup可以不写
 * 9. readonly可以不写
 * 10. color为输入框内的文字颜色,可以不写
 * 11. noborder：是否无边框，缺省值为false；如果为true，则readonly自动设置为true
 *  
 * 修订列表： v1.00 2010-01-26 
 * 			修订人：董一华 
 * 			修订内容：初始版本
 * 
 */

package com.zjht.gmcc.bean.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyFormat;

public class InputTag extends TagSupport {

	private static final long serialVersionUID = -3197887636838457532L;
	private String name;
	private String id;
	private String type;
	private String onchange;
	private String value;
	private int size;
	private int maxlength;
	private boolean disabled;
	private String onkeyup;
	private boolean readonly;
	private String color;
	private boolean noborder;

	public InputTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		StringBuilder html = new StringBuilder();
		String name = this.name;
		String id = this.id;
		String type = this.type;
		String onchange = this.onchange;
		String value = this.value;
		int size = this.size;
		int maxlength = this.maxlength;
		boolean disabled = this.disabled;
		String onkeyup = this.onkeyup;
		boolean readonly = this.readonly;
		String color = this.color;
		boolean noborder = this.noborder;

		if (name == null || name.length() == 0) {
			throw new JspException("InputTag控件name参数值不能为空！");
		}
		if (id == null || id.length() == 0) {
			id = name;
		}
		if (type == null || type.length() == 0) {
			type = "num";
		} else
			type = type.toLowerCase();

		if (size <= 0 && maxlength <= 0) {
			if (size <= 0)
				size = 8;
			if (maxlength <= 0)
				maxlength = 7;
		} else {
			if (size <= 0)
				size = maxlength;
			if (maxlength <= 0)
				maxlength = size;
		}
		String onKeyPress = "";
		if ("currency".equals(type)) {
			onKeyPress = "numberOnly()";
		} else if ("string".equals(type)) {
			onKeyPress = "intOnly()";
		} else {
			onKeyPress = "intOnly()";
		}
		if (noborder) {
			readonly = true;
		}

		try {
			html.append("<input name=\"");
			html.append(name);
			html.append("\" id=\"");

			html.append(id);
			html.append("\" type=\"text\" size=\"");
			html.append(size);
			html.append("\" maxlength=\"");
			html.append(maxlength);
			html.append("\" value=\"");
			if (value != null && value.length() > 0) {
				value = value.trim();
				if ("currency".equals(type)) {
					value = MyFormat.getMoney(Double.parseDouble(value));
				} else if ("string".equals(type)) {

				} else {
					value = MyFormat.getRealNumber(value);
				}
				html.append(value);
			}
			html.append("\" onKeyPress=\"");
			html.append(onKeyPress);
			html.append("\" ");
			if (!MyFormat.isStrNull(onchange)) {
				html.append(" onchange=\"");
				html.append(onchange);
				html.append("\"");
			}
			if (!MyFormat.isStrNull(onkeyup)) {
				html.append(" onkeyup=\"");
				html.append(onkeyup);
				html.append("\"");
			}
			html.append(" style=\"ime-mode:Disabled");
			if (!MyFormat.isStrNull(color)) {
				html.append(";color:");
				html.append(color);
			}
			if (noborder) {
				html.append(";border:0px;background-color:transparent;text-align:center");
			}
			html.append("\"");
			html.append(" onbeforepaste=\"clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\\d]/g,\'\'))\" ");

			if (disabled) {
				html.append(" disabled ");
			}
			if (readonly) {
				html.append(" readonly ");
			}
			html.append("/>");
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMaxlength(int maxlength) {
		this.maxlength = maxlength;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public void setOnkeyup(String onkeyup) {
		this.onkeyup = onkeyup;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setValue(int value) {
		this.value = String.valueOf(value);
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = (disabled != null && "true".equals(disabled.trim().toLowerCase()));
	}

	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	public void setReadonly(String readonly) {
		this.readonly = (readonly != null && "true".equals(readonly.trim().toLowerCase()));
	}

	public void setColor(String color) {
		this.color = color;
	}

	public void setNoborder(boolean noborder) {
		this.noborder = noborder;
	}

	public void setNoborder(String noborder) {
		this.noborder = (noborder != null && "true".equals(noborder.trim().toLowerCase()));
	}
}
